/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ReplaceDialog.java
 *
 * Created on Jul 14, 2011, 7:52:14 PM
 */

package sources;

import java.awt.Color;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author owner
 */
public class ReplaceDialog extends java.awt.Dialog {

    /** Creates new form ReplaceDialog */
    private int index;                                                 //Use to set start search string number.
    private int currentfocusstart;
    private int currentfocusend ;
    private boolean find;

    public ReplaceDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.setResizable(false);
        index =0;                                                 //Use to set start search string number.
        currentfocusstart =0;
        currentfocusend =0;
        find = false;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });
        setLayout(new javax.swing.BoxLayout(this, javax.swing.BoxLayout.Y_AXIS));

        jLabel1.setText("Find what:");
        jLabel1.setMaximumSize(new java.awt.Dimension(61, 14));
        jLabel1.setMinimumSize(new java.awt.Dimension(61, 14));
        jLabel1.setPreferredSize(new java.awt.Dimension(61, 14));
        jPanel1.add(jLabel1);

        jTextField1.setMinimumSize(new java.awt.Dimension(120, 25));
        jTextField1.setPreferredSize(new java.awt.Dimension(120, 25));
        jPanel1.add(jTextField1);

        jButton1.setText("Find Next");
        jButton1.setMaximumSize(new java.awt.Dimension(90, 23));
        jButton1.setMinimumSize(new java.awt.Dimension(90, 23));
        jButton1.setPreferredSize(new java.awt.Dimension(90, 23));
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton1);

        add(jPanel1);

        jLabel2.setText("Replace with:");
        jPanel2.add(jLabel2);

        jTextField2.setMinimumSize(new java.awt.Dimension(120, 25));
        jTextField2.setPreferredSize(new java.awt.Dimension(120, 25));
        jPanel2.add(jTextField2);

        jButton2.setText("Replace");
        jButton2.setMaximumSize(new java.awt.Dimension(85, 23));
        jButton2.setMinimumSize(new java.awt.Dimension(85, 23));
        jButton2.setPreferredSize(new java.awt.Dimension(85, 23));
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jPanel2.add(jButton2);

        add(jPanel2);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /** Closes the dialog */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        setVisible(false);
        dispose();
    }//GEN-LAST:event_closeDialog

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        int endIndex = 0;
           int startIndex = 0;
           //int index = 0;
           int length = Inter_Editor.textpane.getText().length();
           String SearchString = jTextField1.getText();
           String areastring = Inter_Editor.textpane.getText();
           //areastring.trim();
           Pattern p =Pattern.compile("\r\n");                    //replace all \r\n to " " for search string
           Matcher m = p.matcher(areastring);
           areastring = m.replaceAll(" ");

           startIndex = (areastring.indexOf(SearchString, index));
           endIndex = (startIndex + SearchString.length());

                if(index < length && !SearchString.equals("") && Inter_Editor.textpane.getText().indexOf(SearchString,index) >= 0){
                    Inter_Editor.textpane.setSelectionStart(startIndex);
                    Inter_Editor.textpane.setSelectionEnd(endIndex);
                    Inter_Editor.textpane.setSelectedTextColor(Color.RED);
                    index = endIndex;
                    currentfocusstart = startIndex;
                    currentfocusend = endIndex;
                    jTextField1.requestFocus();
                    Inter_Editor.textpane.requestFocus();
                    find = true;
                }
                if(Inter_Editor.textpane.getText().indexOf(SearchString,index) < 0){
                    index = 0;
                    
                }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
            jButton1ActionPerformed(evt);
            //String SearchString = jTextField1.getText();
           // if(SearchString.equals("") && Inter_Editor.textpane.getText().indexOf(SearchString,index) >= 0 ){
                   //Inter_Editor.textpane.replaceRange(jTextField2.getText(), currentfocusstart, currentfocusend);
                   
                    //.replaceRange(jTextField2.getText(), currentfocusstart, currentfocusend);
                   if(find){
                        Inter_Editor.textpane.replaceSelection(jTextField2.getText());
                        jTextField2.requestFocus();
                        Inter_Editor.textpane.requestFocus();
                        find = false;
                    }
         //       }
           // }

           
    }//GEN-LAST:event_jButton2ActionPerformed

    /**
    * @param args the command line arguments
    */
  


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration//GEN-END:variables

}
